<template>
	<view class="contanier">
		<view class="bgc-color">
			<view class="header">
				<view class="header-img">
					<u-avatar :src="userInfo.avatar" shape="circle" size="56"></u-avatar>
				</view>
				<view class="header-info">
					<view class="header-member">
						<text style="color: #FFFFFF;">{{userInfo.nickname}}</text>
						<image src="/static/member.png" style="width: 30rpx; height: 30rpx;margin-left: 8rpx;"></image>
					</view>
					<!-- <text style="color: #ebc795;font-size: 14px; margin-top: 5rpx;">{{end_time}}</text> -->
				</view>
			</view>
			<view class="tabs" v-if="is_type_default">
				<view class="tabs-card left" @click="doclick">
					<image :src="tabImg?'/static/left-active.png':'/static/left-show.png'" class="img-box"></image>
					<text class="tabs-text" :class="{activecolor:activecolor,showcolor:showcolor}">房源发布会员</text>
				</view>
				<view class="tabs-card right" @click="doset">
					<image :src="tabImg?'/static/right-show.png':'/static/right-active.png'" class="img-box">
					</image>
					<text class="tabs-text" :class="{showcolor:!showcolor,activecolor:!activecolor}">房源浏览会员</text>
				</view>

			</view>
			<view class="tabs" v-else>
				<view class="tabs-card left" @click="doclick" v-if="type == 1">
					<image :src="tabImg?'/static/left-active.png':'/static/left-show.png'" class="img-box"></image>
					<text class="tabs-text" :class="{activecolor:activecolor,showcolor:showcolor}">房源发布会员</text>
				</view>
				<view class="tabs-card right" @click="doset" v-if="type == 2">
					<image :src="tabImg?'/static/right-show.png':'/static/right-active.png'" class="img-box">
					</image>
					<text class="tabs-text" :class="{showcolor:!showcolor,activecolor:!activecolor}">房源浏览会员</text>
				</view>
			</view>
		</view>
		<view class="apply-card">
			<scroll-view scroll-x="true" class="list-box">
				<view class="card-box" :class="currentIndex== index?'active':''" v-for="(item,index) in cardList"
					:key="index" @click="doSelected(item,index)">
					<view style="display: flex;flex-direction: column;align-items: center;">
						<text style="font-size: 16px;" v-if="item.type == 1">
							发布会员
						</text>
						<text style="font-size: 16px;" v-if="item.type == 2">
							浏览会员
						</text>
						<text style="font-size: 20px; color: #5e441a;">{{item.title}}</text>
					</view>
					<text style="font-size: 20px; color: #9a7640;">￥{{item.money}}</text>
				</view>
			</scroll-view>
		</view>
		<view class="equity">
			<text style="color:#9a7640; font-size: 16px;">会员权益</text>
			<view class="">
				<text style="font-size: 14px;">{{mark}}</text>
			</view>
		</view>
		<view class="btn">
			<text style="font-size: 14px;margin-left: 46rpx;color: #767676;">{{pitch_type}}:{{pitch}}</text>
			<button type="default" style="color: #4f3d23;background-color: #e1bf86;"
				@click="btn_submit()">确认协议并开通</button>
			<view
				style="font-size: 14px;margin-top: 10px;text-align: center;color: #767676;display: flex;justify-content: center;">
				<u-radio-group v-model="isagreement">
					<u-radio shape="circle" @change="doagreement()"></u-radio>
				</u-radio-group>
				开通前请阅读
				<text style="color: #007AFF;" @click="jumpxy()">《会员服务协议》</text>
			</view>
		</view>
		<u-overlay :show="showstatus" @click="showstatus = false">
			<view class="u-content"
				style="position: relative; top: 10%;background: #ccc;height: 80%; width: 100%; margin: auto; border-radius: 5px;overflow-x: hidden;overflow-y: scroll;">
				<view style="display: flex;justify-content: center;margin: 10px;">会员服务协议</view>
				<u-parse :content="s_content"></u-parse>
			</view>
			<u-button @click="showstatus = true">确认</u-button>
		</u-overlay>

	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				src: 'https://img2.baidu.com/it/u=491480542,2485358247&fm=26&fmt=auto',
				text: '微信昵称',
				date: '2020-01-01',
				tabImg: true,
				currentIndex: 0,
				activecolor: true,
				showcolor: false,
				pitch: '',
				pitch_type: '发布会员',
				type: 1,
				cardList: [],
				mark: '',
				selectId: 0,
				end_time: '',
				showstatus: false,
				isagreement: 1,
				xytitle: '会员服务协议',
				s_content: '',
				is_type_default: true
			};
		},
		onLoad(option) {
			console.log(option)
			if (option.type == 1) {
				this.is_type_default = false;
				this.tabImg = true;
				this.activecolor = true;
				this.showcolor = false;
				this.type = 1;
				this.pitch_type = "发布会员"
			} else if (option.type == 2) {
				this.is_type_default = false;
				this.tabImg = false;
				this.showcolor = true;
				this.activecolor = false;
				this.type = 2;
				this.pitch_type = "浏览会员"
			}

			this.loadData();

		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		methods: {
			doclick() {
				this.tabImg = true;
				this.activecolor = true;
				this.showcolor = false;
				this.type = 1;
				this.loadData();
				this.pitch_type = "发布会员"
			},
			jumpxy() {
				let that = this;
				this.showstatus = true;
				that.http("applet_xieyi_index").then(res => {

					that.s_content = res.data.xy[0]['s_content'];
				})
			},
			doagreement(e) {
				console.log(e);
			},
			doset() {
				this.tabImg = false;
				this.showcolor = true;
				this.activecolor = false;
				this.type = 2;
				this.loadData()
				this.pitch_type = "浏览会员"
			},
			// 选中会员卡点击事件
			doSelected(item, index) {
				console.log(index);
				this.currentIndex = index;
				this.pitch = item.title;
				this.mark = item.mark
				this.selectId = item.id
			},
			async loadData() {
				let that = this;
				that.http("applet_member_get_type", {
					type: that.type
				}).then(res => {
					if (res.ec === 200) {
						that.cardList = res.data.config;
						that.mark = res.data.config[0].mark;
						that.pitch = res.data.config[0].title;
						that.selectId = res.data.config[0].id
						that.end_time = res.data.info.end_time
					}
				})
			},
			btn_submit() {
				let that = this;
				console.log(that.isagreement)
				if (that.isagreement == 1) {
					uni.showToast({
						title: "请勾选服务协议",
						icon: "success"
					})
					return;
				}
				that.http("applet_member_card_money", {
					id: that.selectId,
					type: this.type
				}).then(res => {
					console.log(res)
					if (res.ec == 200) {
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							paySign: res.data.paySign,
							signType: res.data.signType,
							success(res) {
								console.log(res)
								uni.showToast({
									title: "支付成功",
									icon: "success",
									success() {
										uni.navigateBack({

										})
									}
								})
							},
							fail(error) {
								console.log(error)
								uni.showToast({
									title: "支付失败",
									icon: "error"
								})
							}
						})
					}
				})
			}
		}

	}
</script>

<style lang="scss">
	.contanier {
		width: 100vw;
		height: 100vh;

		.bgc-color {
			width: 100%;
			height: 260rpx;
			background-color: #303133;
			position: relative;
		}

		.header {
			position: absolute;
			top: 20rpx;
			display: flex;
			align-items: center;
			margin-left: 24rpx;

			.header-info {
				display: flex;
				flex-direction: column;
				margin-left: 24rpx;
			}
		}

		.tabs {
			position: absolute;
			bottom: 0;
			width: 100%;
			display: flex;
			justify-content: space-between;

			.tabs-card {
				position: relative;
				width: 51%;
				height: 100rpx;
				text-align: center;
				line-height: 100rpx;

				.img-box {
					width: 100%;
					height: 100rpx;
				}

				.tabs-text {
					position: absolute;
					top: 0;
					left: 50rpx;
					font-size: 20px;
				}

				.activecolor {
					color: #6f5122;
				}

				.showcolor {
					color: #dbbdac;
				}
			}

		}

		.list-box {
			width: 100%;
			margin-top: 36rpx;
			margin-bottom: 36rpx;
			overflow: hidden;
			white-space: nowrap;

			.card-box {
				display: inline-block;
				text-align: center;
				line-height: 100rpx;
				margin: 0 14rpx;
				width: 240rpx;
				height: 300rpx;
				border-radius: 14rpx;
				border: 1px solid #d5d5d5;
				background-color: #f4f4f4;
			}

			.active {
				border: 1px solid #ebc795;
				background-color: #fff2e1;
			}
		}



		.equity {
			padding-left: 10rpx;
		}

		.btn {
			position: absolute;
			bottom: 20px;
			width: 100%;
			background-color: #fff;

			button {
				width: 95%;
				margin: auto;
				background-color: #F9AE3D;
				border-radius: 70rpx;
			}
		}
	}

	.u-radio-group {
		flex: 0 !important;

	}
</style>
